
//获取对象
let oDiv=document.querySelectorAll('.notice')
let oSint=document.querySelector('.header-right')
let oSi=document.querySelector('.signOut')
let oBc=document.querySelector('.body-container')
let oTbody=document.querySelector('.tbody1')
let oTbody1=document.querySelector('.tbody2')
let page=1; //页码
let count=6; //每一页多少条
let pagination=document.querySelector('.pag1')
let pagination1=document.querySelector('.pag2')
let oForm=document.querySelectorAll('.body-form')
//导航栏点击事件
for(let i=0;i<oDiv.length;i++){
    oDiv[i].onclick=()=>{
        for(let i=0;i<oDiv.length;i++){
            oDiv[i].classList.remove('active')
            oForm[i].style.display='none'
        }
        oDiv[i].classList.add('active')
        oForm[i].style.display='block'

    }
}
//退出系统
oSint.onclick=()=>{
    if(oSi.style.display=='block'){
        oSi.style.display='none'
    }else{
        oSi.style.display='block'
    }
}

let str=getCookie('upeople')
function toData(){


    $.ajax({
        url:'/toData',
        data:{
            page:page,
            count:count,
        },
        method:'post',
        success:(res)=>{
            oTbody.innerHTML=''
            pagination.innerHTML=`
                    <li>
                        <a class="left" aria-label="Previous">
                        <span class="left" aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li>
                        <a class="right" aria-label="Next">
                        <span class="right" aria-hidden="true">&raquo;</span>
                        </a>
                    </li>`
            res.data.forEach(v => {
                let oTr=document.createElement('tr')
                oTr.innerHTML=`<td>
                <h5>${v.n_title}</h5>
                <p style="float: left;">${v.n_content}</p>
                <span style="float: right;">${new Date(v.n_time).toLocaleDateString()}</span>
                </td>`
                oTbody.appendChild(oTr)
            });
            //渲染页码
            let pageCount=Math.ceil(res.total.total/count)
            for(let i=1;i<=pageCount;i++){
                let oLi=document.createElement('li')
                oLi.innerHTML=`<a class="page">${i}</a>`
                if(i==page){
                    oLi.classList.add('active')
                }
                pagination.insertBefore(oLi,pagination.lastElementChild)
            }
            
            
        }
        });
}
toData()
let oHerder=document.querySelector('.header-name')
function getstudent(){
    oHerder.innerHTML=`
    <span>▼</span>`
    $.ajax({
        url:'/getData',
        data:{
            loginId:str
        },
        method:'post',
        success:(res)=>{
                let oData=res.data.concat(res.data2)
                let Stdor=Object.assign(oData[0],oData[1])
                setCookie('ID',Stdor.s_id)
                setCookie('Drom',Stdor.s_d_id)
                oTbody1.innerHTML=''
                let oTr=document.createElement('tr')
                let oSpan=document.createElement('span')
                
                oSpan.innerHTML=`${Stdor.s_name}`
                oHerder.insertBefore(oSpan,oHerder.lastElementChild)
                oTr.innerHTML=`
                    <td>${Stdor.s_id}</td>
                    <td>${Stdor.s_name}</td>
                    <td>${Stdor.s_user}</td>
                    <td>${Stdor.s_d_id}</td>
                    <td>${Stdor.d_cost}</td>
                    <td>${Stdor.d_status}</td>
                    <td>
                        <a class="payment" data-toggle="modal" data-target="#myModal">查看缴费记录</a>
                        <a class="recharge" data-toggle="modal" data-target="#myRecharge">充值</a>
                    </td>
                                `
                oTbody1.appendChild(oTr)
        }
        });
}

getstudent()




//切换页码
chanFn=()=>{
    if(event.target.className=='page'){
        page=Number(event.target.innerText)
        toData()
    }
}
pagination.addEventListener('click',chanFn)
//上一页下一页 
toLeft=()=>{
    if(event.target.className=='left'){
        if(page>1){
            page--
            toData()
        }
    }
}
pagination.addEventListener('click',toLeft)

toRight=()=>{
    if(event.target.className=='right'){
        if(page<pagination.children.length-2){
            page++

            toData()
        }
        
    }
}
pagination.addEventListener('click',toRight)

let oTbody2=document.querySelector('.tbody3')

//缴费记录
let ID=getCookie('ID')
payFn=()=>{
    oTbody2.innerHTML=''
    if(event.target.className=='payment')
    $.ajax({
        url:'/payment',
        data:{
            s_id:ID
        },
        method:'post',
        success:(res)=>{
            console.log(res)
            res.forEach(v=>{
                let oTr=document.createElement('tr')
                oTr.innerHTML=`
                    <td>${v.p_money} 元</td>
                    <td>${new Date(v.p_time).toLocaleDateString()}</td>`
                oTbody2.appendChild(oTr)
            })
        }
    })
}
oTbody1.addEventListener('click',payFn)
//充值
let rechargeBtn=document.querySelector('.rechargeBtn')
let oImoney=document.querySelector('#money')
let oDate=new Date().toLocaleDateString()
let s_drom=getCookie('Drom')
console.log(oDate)
rechargeBtn.onclick=()=>{
    $.ajax({
        url:'/rechargeBtn',
        data:{
            money:oImoney.value,
            time:oDate,
            s_id:ID,
            s_drom:s_drom
        },
        method:'post',
        success:(res)=>{
            if(res){
                oImoney.value=''
                getstudent()
                payFn()
                alert('充值成功！')
                
            }
        }
    })
}
//退出登录
let signOut=document.querySelector('.signOut')
signOut.onclick=()=>{
    window.location.href='/login.html'
    
}